# -*- coding: utf-8 -*-
# models.py

# All standard Django fields as well as GeoDjango geometry fields and the GeoManager() can be
# imported from django.contrib.gis.db
from django.contrib.gis.db import models

# Used to display html 'help text' links within Admin App
from django.utils.safestring import mark_safe

class WorldBorders(models.Model):
    """
    
    A geographic model based on the v3 of the simplified world borders multipolygon shapefile
    from http://thematicmapping.org/downloads/world_borders.php.
    
    Field names, Django types, and max_lengths were autogenerated using the ogrinspect utility with hand
    edits to add alternative field names and help_text.
    
    Imported using LayerMapping (requires GDAL) called within the load_data.py script provided
    within this sample project.
    
    All fields match the source dataset, an ESRI format shapefile made up of several related files:
    .shp - holds the vector data that is to be stored in the MultiPolygonField field named'geometry'.
    .shx - spatial index file for geometries stored in the .shp.
    .dbf - database file for holding attribute data (can be opened in excel and open office).
    .prj - contains the spatial reference information for the geometries stored in the .shp
      

    """
    
    # Regular Django fields corresponding to the attributes in the
    # world borders shapefile
    name = models.CharField(max_length=50)
    area = models.IntegerField(help_text="Area of Country in SQ meters")
    pop2005 = models.IntegerField('Population 2005')
    fips = models.CharField('FIPS Code', max_length=2, help_text=mark_safe('<a href="http://www.census.gov/geo/www/fips/fips.html">Federal Information Processing Standard Code</a>'))
    iso2 = models.CharField('2 Digit ISO', max_length=2, help_text=mark_safe('<a href="http://www.iso.org/">International Organization for Standardization</a>' ))
    iso3 = models.CharField('3 Digit ISO', max_length=3,help_text=mark_safe('<a href="http://www.iso.org/">International Organization for Standardization</a>' ))
    un = models.IntegerField('United Nations Code')
    region = models.IntegerField('Region Code')
    subregion = models.IntegerField('Sub-Region Code')
    lon = models.FloatField('Longitude',help_text="Longitude of centerpoint")
    lat = models.FloatField('Latitude',help_text="Latitude of centerpoint")
    
    # GeoDjango-specific: a geometry field (MultiPolygonField), and
    # overriding the default manager with a GeoManager instance.
    geometry = models.MultiPolygonField('Country Border',srid=4326)
    objects = models.GeoManager()

    # So the model is pluralized correctly in the admin.
    class Meta:
        verbose_name_plural = "World Borders"

    # Returns the string representation of the model.        
    def __unicode__(self):
        return self.name